package gruppe5.dame.model.tester;

import java.util.List;
import java.util.Scanner;

import gruppe5.dame.model.DameModel;
import gruppe5.dame.model.Pawn;
import gruppe5.dame.model.types.DameGameType;
import gruppe5.dame.model.types.PawnDirection;

public class DameModelTester 
{
	private DameModel model;
	
	private final int gridSize;
	
	
	public DameModelTester(DameModel _model) 
	{
		this.model = _model;
		this.gridSize = model.getGridSize();
	}



	/**
	 * Gibt einen String zurueck der das Spielbrett mit den Spielsteinen graphisch darstellt.
	 */
	public String toString()
	{	
		Pawn[][] pawnMatrix = new Pawn[gridSize][gridSize];
		for(Pawn pawn : this.model.getAllPawns())
		{
			pawnMatrix[pawn.getX()][pawn.getY()] = pawn;
		}
		
		String objectString = new String();
		
		for(int i = 0; i < gridSize; i++)
		{
			for(int j = 0; j < gridSize; j++)
			{
				if((i+j)%2 == 0)
				{
					objectString += ".";
				}else if(pawnMatrix[i][j] == null) {
					objectString += "   ";
				}else if(pawnMatrix[i][j].isBlack()) {
					objectString += "B" +pawnMatrix[i][j].getId();
				}else if(pawnMatrix[i][j].isWhite()) {
					objectString += "W" +pawnMatrix[i][j].getId();
				}
				if(pawnMatrix[i][j] != null)
				{
					if(pawnMatrix[i][j].isDame()) 
					{
						objectString += "*";
					}
				}
				objectString += "    ";
			}
			objectString += "\n\n";
		}
		return objectString;
	}

	
	
	/************************************************************************************
	 ************************************MAIN********************************************
	 ************************************************************************************/
	
	/**
	 * Main Funktion der Spielelogik.
	 * Wird nur zu Testzwecken benutzt. Z.B. Ausgabe des Spielbretts.
	 * @param args leer.
	 */
	public static void main(String[] args) 
	{
		DameModel model = new DameModel(DameGameType.SMALL);
		DameModelTester tester = new DameModelTester(model);
		
		/*************TESTS****************/
		System.out.println(tester.toString());
		
		
		while(true)
		{
			System.out.print("pawnsThathMustMove: ");
			model.getAllPawns();
			for(Pawn pawn : model.getPawnsThatMustMove())
			{
				System.out.print(" " +pawn.getId());
			}
			System.out.println();
			
			@SuppressWarnings("resource")
			Scanner scan = new Scanner(System.in);
			System.out.println("Spielstein Auswaehlen: ");
			int pawnId = scan.nextInt();
			System.out.println("availableMoves fuer pawn: " +pawnId);
			
			List<PawnDirection> moveList = null;
			for(Pawn pawn : model.getAllPawns())
			{
				if(pawn.getId() == pawnId)
				{
					moveList = pawn.getAvailableMoves();
				}
			}
			
			
			int i = 0;
			for(PawnDirection direction : moveList)
			{
				System.out.print(i+1 +": ");
				System.out.println(direction);
				i++;
			}
			if(moveList.size() <= 0)
			{
				System.out.println("SPIELSTEIN BLOCKIERT");
				continue;
			}
			
			System.out.println("Richtung Auswaehlen: ");
			int directionId = scan.nextInt();

			if(moveList.size() < directionId)
			{
				System.out.println("UNGUELTIGER SPIELZUG");
				continue;
			}
			
			model.movePawn(pawnId, moveList.get(directionId-1));
			
			System.out.println(tester.toString());

			System.out.println();
		}
		
		/*************TESTS_ENDE****************/

		
	}

}
